package com.itlong.cloud.controller.inspect;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.DTO.property.PropertyInspectOperateDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyInspectOperateVO;
import com.itlong.cloud.PlatformConstants;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.inspect.IPropertyInspectOperateServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.export.ExportUtil;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import com.itlong.cloud.utils.words.NumberHandlerUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      物业-巡检管理-基本操作控制器
 * </desc>
 *
 * @createDate 2019/12/26
 */
@RestController
@RequestMapping("/inspect")
@RefreshScope
public class PropertyInspectOperateController {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyInspectOperateController.class);

    @Value("${" + PlatformConstants.FILE_FTP_HOST + "}")
    public String FILE_FTP_HOST;

    @Value("${" + PlatformConstants.FILE_FTP_PORT + "}")
    public String FILE_FTP_PORT;

    @Value("${" + PlatformConstants.FILE_FTP_USERNAME + "}")
    public String FILE_FTP_USERNAME;

    @Value("${" + PlatformConstants.FILE_FTP_PASSWORD + "}")
    public String FILE_FTP_PASSWORD;

    @Value("${" + PlatformConstants.FILE_ACCESS_URL + "}")
    public String FILE_ACCESS_URL;

    @Autowired
    private IPropertyInspectOperateServiceClient iPropertyInspectOperateServiceClient;

    /**
     * <desc>
     *      列表查询历史数据信息
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/12/26
     */
    @ResponseBody
    @PostMapping(path = "historicalData/getPage")
    public Object getHistoricalDataPage(HttpServletRequest request){
        try {
            PropertyInspectOperateDTO propertyInspectOperateDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectOperateDTO.class, request);
            if (propertyInspectOperateDTO == null || StringUtils.isBlank(propertyInspectOperateDTO.getProjectId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            propertyInspectOperateDTO.setPageMark("pageMark");
            Page<PropertyInspectOperateVO> propertyInspectOperateVOPage = iPropertyInspectOperateServiceClient.getHistoricalDataPage(propertyInspectOperateDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), propertyInspectOperateVOPage);
        } catch (Exception e) {
            throw new DataAccessException("【物业-巡检管理-基本操作】列表查询历史数据信息失败",e);
        }
    }

    /**
     * <desc>
     *      导出历史数据信息
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/12/26
     */
    @AuthAccessRequired
    @ResponseBody
    @PostMapping(path = "historicalData/export")
    public Object exportHistoricalData(HttpServletRequest request){
        try {
            PropertyInspectOperateDTO propertyInspectOperateDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectOperateDTO.class, request);
            if (propertyInspectOperateDTO == null || StringUtils.isBlank(propertyInspectOperateDTO.getProjectId()) || StringUtils.isBlank(propertyInspectOperateDTO.getExportItems())
                    || StringUtils.isBlank(propertyInspectOperateDTO.getExportItemNames())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<PropertyInspectOperateVO> propertyInspectOperateVOPage = iPropertyInspectOperateServiceClient.getHistoricalDataPage(propertyInspectOperateDTO);
            List<PropertyInspectOperateVO> propertyInspectOperateVOList = propertyInspectOperateVOPage.getRows();
            if (propertyInspectOperateVOList == null || propertyInspectOperateVOList.isEmpty()) {
                return new MessageVO(PropertyErrorCodeEnum.EXPORT_DATA_EMPTY_ERR.getErrorCode());
            }
            if (propertyInspectOperateVOList.size() > 2000) {
                return new MessageVO(PropertyErrorCodeEnum.EXPORT_DATA_TOO_MANY_ERR.getErrorCode());
            }

            propertyInspectOperateDTO.setExportItemNames("序号," + propertyInspectOperateDTO.getExportItemNames());
            propertyInspectOperateDTO.setExportItems("serialNum," + propertyInspectOperateDTO.getExportItems());
            String exportFileUrl = ExportUtil.exportByItemsAndNames(propertyInspectOperateVOList, propertyInspectOperateDTO.getExportItemNames(), propertyInspectOperateDTO.getExportItems(),
                    (short) 420, 16, propertyInspectOperateDTO.getProjectId(), FILE_FTP_HOST, FILE_FTP_USERNAME, FILE_FTP_PASSWORD, FILE_FTP_PORT, FILE_ACCESS_URL);

            if (StringUtils.isBlank(exportFileUrl)){
                return new MessageVO(PropertyErrorCodeEnum.PROJECT_EXPORT_FILE_SAVE_ERR.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), (Object) exportFileUrl);
        } catch (Exception e) {
            throw new DataAccessException("【物业-巡检管理-基本操作】导出历史数据信息失败",e);
        }
    }

    /**
     * <desc>
     *      列表查询巡检报告数据信息
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/12/30
     */
    @ResponseBody
    @PostMapping(path = "report/getPage")
    public Object getReportPage(HttpServletRequest request){
        try {
            PropertyInspectOperateDTO propertyInspectOperateDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectOperateDTO.class, request);
            if (propertyInspectOperateDTO == null || StringUtils.isBlank(propertyInspectOperateDTO.getProjectId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            propertyInspectOperateDTO.setPageMark("pageMark");
            Page<PropertyInspectOperateVO> propertyInspectOperateVOPage = iPropertyInspectOperateServiceClient.getReportPage(propertyInspectOperateDTO);
            String expand = null;
            if (propertyInspectOperateVOPage.getRows() != null && !propertyInspectOperateVOPage.getRows().isEmpty()) {
                Map<String, Object> expandMap = propertyInspectOperateVOPage.getRows().get(0).getExpandMap();
                expand = JSON.toJSONString(expandMap);
            }
            return new MessageVO(expand, BaseErrorCodeEnum.SUCCESS.getErrorCode(), propertyInspectOperateVOPage);
        } catch (Exception e) {
            throw new DataAccessException("【物业-巡检管理-基本操作】列表查询巡检报告数据信息失败",e);
        }
    }

    /**
     * <desc>
     *      导出巡检报告数据信息
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2019/12/31
     */
    @AuthAccessRequired
    @ResponseBody
    @PostMapping(path = "report/export")
    public Object exportReportData(HttpServletRequest request){
        try {
            PropertyInspectOperateDTO propertyInspectOperateDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectOperateDTO.class, request);
            if (propertyInspectOperateDTO == null || StringUtils.isBlank(propertyInspectOperateDTO.getProjectId()) || StringUtils.isBlank(propertyInspectOperateDTO.getExportItems())
                    || StringUtils.isBlank(propertyInspectOperateDTO.getExportItemNames())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<PropertyInspectOperateVO> propertyInspectOperateVOPage = iPropertyInspectOperateServiceClient.getReportPage(propertyInspectOperateDTO);
            List<PropertyInspectOperateVO> propertyInspectOperateVOList = propertyInspectOperateVOPage.getRows();
            if (propertyInspectOperateVOList == null || propertyInspectOperateVOList.isEmpty()) {
                return new MessageVO(PropertyErrorCodeEnum.EXPORT_DATA_EMPTY_ERR.getErrorCode());
            }
            if (propertyInspectOperateVOList.size() > 2000) {
                return new MessageVO(PropertyErrorCodeEnum.EXPORT_DATA_TOO_MANY_ERR.getErrorCode());
            }

            propertyInspectOperateDTO.setExportItemNames("序号," + propertyInspectOperateDTO.getExportItemNames());
            propertyInspectOperateDTO.setExportItems("serialNum," + propertyInspectOperateDTO.getExportItems());
            String exportFileUrl = ExportUtil.exportByItemsAndNames(propertyInspectOperateVOList, propertyInspectOperateDTO.getExportItemNames(), propertyInspectOperateDTO.getExportItems(),
                    (short) 420, 16, propertyInspectOperateDTO.getProjectId(), FILE_FTP_HOST, FILE_FTP_USERNAME, FILE_FTP_PASSWORD, FILE_FTP_PORT, FILE_ACCESS_URL);

            if (StringUtils.isBlank(exportFileUrl)){
                return new MessageVO(PropertyErrorCodeEnum.PROJECT_EXPORT_FILE_SAVE_ERR.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), (Object) exportFileUrl);
        } catch (Exception e) {
            throw new DataAccessException("【物业-巡检管理-基本操作】导出巡检报告数据信息失败",e);
        }
    }

    /**
     * <desc>
     *      查询统计数据信息
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2020/01/02
     */
    @ResponseBody
    @PostMapping(path = "statistics/getData")
    public Object getData(HttpServletRequest request){
        try {
            PropertyInspectOperateDTO propertyInspectOperateDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectOperateDTO.class, request);
            if (propertyInspectOperateDTO == null || StringUtils.isBlank(propertyInspectOperateDTO.getProjectId())
                    || !NumberHandlerUtil.isInRange(propertyInspectOperateDTO.getStatType(), 1, 4)) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            List<PropertyInspectOperateVO> propertyInspectOperateVOList = iPropertyInspectOperateServiceClient.getStatisticsData(propertyInspectOperateDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), propertyInspectOperateVOList);
        } catch (Exception e) {
            throw new DataAccessException("【物业-巡检管理-基本操作】查询统计数据信息失败",e);
        }
    }

    /**
     * <desc>
     *      巡检日历列表查询
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2020/01/06
     */
    @ResponseBody
    @PostMapping(path = "calendar/getPage")
    public Object getCalendarPage(HttpServletRequest request){
        try {
            PropertyInspectOperateDTO propertyInspectOperateDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectOperateDTO.class, request);
            if (propertyInspectOperateDTO == null || StringUtils.isBlank(propertyInspectOperateDTO.getProjectId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            propertyInspectOperateDTO.setPageMark("pageMark");
            Page<PropertyInspectOperateVO> propertyInspectOperateVOPage = iPropertyInspectOperateServiceClient.getCalendarPage(propertyInspectOperateDTO);
            String expand = null;
            if (propertyInspectOperateVOPage.getRows() != null && !propertyInspectOperateVOPage.getRows().isEmpty()) {
                Map<String, Object> expandMap = propertyInspectOperateVOPage.getRows().get(0).getExpandMap();
                expand = JSON.toJSONString(expandMap);
            }
            return new MessageVO(expand, BaseErrorCodeEnum.SUCCESS.getErrorCode(), propertyInspectOperateVOPage);
        } catch (Exception e) {
            throw new DataAccessException("【物业-巡检管理-基本操作】巡检日历列表查询失败",e);
        }
    }

    /**
     * <desc>
     *      导出巡检日历数据信息
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2020/01/06
     */
    @AuthAccessRequired
    @ResponseBody
    @PostMapping(path = "calendar/export")
    public Object exportCalendarData(HttpServletRequest request){
        try {
            PropertyInspectOperateDTO propertyInspectOperateDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectOperateDTO.class, request);
            if (propertyInspectOperateDTO == null || StringUtils.isBlank(propertyInspectOperateDTO.getProjectId()) || StringUtils.isBlank(propertyInspectOperateDTO.getExportItems())
                    || StringUtils.isBlank(propertyInspectOperateDTO.getExportItemNames())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<PropertyInspectOperateVO> propertyInspectOperateVOPage = iPropertyInspectOperateServiceClient.getCalendarPage(propertyInspectOperateDTO);
            List<PropertyInspectOperateVO> propertyInspectOperateVOList = propertyInspectOperateVOPage.getRows();
            if (propertyInspectOperateVOList == null || propertyInspectOperateVOList.isEmpty()) {
                return new MessageVO(PropertyErrorCodeEnum.EXPORT_DATA_EMPTY_ERR.getErrorCode());
            }
            if (propertyInspectOperateVOList.size() > 2000) {
                return new MessageVO(PropertyErrorCodeEnum.EXPORT_DATA_TOO_MANY_ERR.getErrorCode());
            }

            propertyInspectOperateDTO.setExportItemNames("序号," + propertyInspectOperateDTO.getExportItemNames());
            propertyInspectOperateDTO.setExportItems("serialNum," + propertyInspectOperateDTO.getExportItems());
            String exportFileUrl = ExportUtil.exportByItemsAndNames(propertyInspectOperateVOList, propertyInspectOperateDTO.getExportItemNames(), propertyInspectOperateDTO.getExportItems(),
                    (short) 420, 14, propertyInspectOperateDTO.getProjectId(), FILE_FTP_HOST, FILE_FTP_USERNAME, FILE_FTP_PASSWORD, FILE_FTP_PORT, FILE_ACCESS_URL);

            if (StringUtils.isBlank(exportFileUrl)){
                return new MessageVO(PropertyErrorCodeEnum.PROJECT_EXPORT_FILE_SAVE_ERR.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), (Object) exportFileUrl);
        } catch (Exception e) {
            throw new DataAccessException("【物业-巡检管理-基本操作】导出巡检日历数据信息失败",e);
        }
    }

    /**
     * <desc>
     *      查看巡检日历数据信息详情
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2020/01/06
     */
    @AuthAccessRequired
    @ResponseBody
    @PostMapping(path = "calendar/getDetails")
    public Object getCalendarDetails(HttpServletRequest request){
        try {
            PropertyInspectOperateDTO propertyInspectOperateDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectOperateDTO.class, request);
            if (propertyInspectOperateDTO == null || StringUtils.isBlank(propertyInspectOperateDTO.getProjectId()) || StringUtils.isBlank(propertyInspectOperateDTO.getDate())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<PropertyInspectOperateVO> propertyInspectOperateVOPage = iPropertyInspectOperateServiceClient.getCalendarDetails(propertyInspectOperateDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), propertyInspectOperateVOPage);
        } catch (Exception e) {
            throw new DataAccessException("【物业-巡检管理-基本操作】查看巡检日历数据信息详情失败",e);
        }
    }

    /**
     * <desc>
     *      报警数据列表查询
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2020/01/07
     */
    @ResponseBody
    @PostMapping(path = "alarm/getPage")
    public Object getAlarmPage(HttpServletRequest request){
        try {
            PropertyInspectOperateDTO propertyInspectOperateDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectOperateDTO.class, request);
            if (propertyInspectOperateDTO == null || StringUtils.isBlank(propertyInspectOperateDTO.getProjectId())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            propertyInspectOperateDTO.setPageMark("pageMark");
            Page<PropertyInspectOperateVO> propertyInspectOperateVOPage = iPropertyInspectOperateServiceClient.getAlarmPage(propertyInspectOperateDTO);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), propertyInspectOperateVOPage);
        } catch (Exception e) {
            throw new DataAccessException("【物业-巡检管理-基本操作】报警数据列表查询失败",e);
        }
    }

    /**
     * <desc>
     *      导出报警数据信息
     * </desc>
     *
     * @return 标准格式化响应结果
     * @author wangzhi
     * @createDate 2020/01/07
     */
    @AuthAccessRequired
    @ResponseBody
    @PostMapping(path = "alarm/export")
    public Object exportAlarmData(HttpServletRequest request){
        try {
            PropertyInspectOperateDTO propertyInspectOperateDTO = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectOperateDTO.class, request);
            if (propertyInspectOperateDTO == null || StringUtils.isBlank(propertyInspectOperateDTO.getProjectId()) || StringUtils.isBlank(propertyInspectOperateDTO.getExportItems())
                    || StringUtils.isBlank(propertyInspectOperateDTO.getExportItemNames())) {
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<PropertyInspectOperateVO> propertyInspectOperateVOPage = iPropertyInspectOperateServiceClient.getAlarmPage(propertyInspectOperateDTO);
            List<PropertyInspectOperateVO> propertyInspectOperateVOList = propertyInspectOperateVOPage.getRows();
            if (propertyInspectOperateVOList == null || propertyInspectOperateVOList.isEmpty()) {
                return new MessageVO(PropertyErrorCodeEnum.EXPORT_DATA_EMPTY_ERR.getErrorCode());
            }
            if (propertyInspectOperateVOList.size() > 2000) {
                return new MessageVO(PropertyErrorCodeEnum.EXPORT_DATA_TOO_MANY_ERR.getErrorCode());
            }

            propertyInspectOperateDTO.setExportItemNames("序号," + propertyInspectOperateDTO.getExportItemNames());
            propertyInspectOperateDTO.setExportItems("serialNum," + propertyInspectOperateDTO.getExportItems());
            String exportFileUrl = ExportUtil.exportByItemsAndNames(propertyInspectOperateVOList, propertyInspectOperateDTO.getExportItemNames(), propertyInspectOperateDTO.getExportItems(),
                    (short) 420, 16, propertyInspectOperateDTO.getProjectId(), FILE_FTP_HOST, FILE_FTP_USERNAME, FILE_FTP_PASSWORD, FILE_FTP_PORT, FILE_ACCESS_URL);

            if (StringUtils.isBlank(exportFileUrl)){
                return new MessageVO(PropertyErrorCodeEnum.PROJECT_EXPORT_FILE_SAVE_ERR.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), (Object) exportFileUrl);
        } catch (Exception e) {
            throw new DataAccessException("【物业-巡检管理-基本操作】导出报警数据信息失败",e);
        }
    }

}
